using System.Collections;
using System.Collections.Generic;
using Cysharp.Threading.Tasks;
using UnityEngine;
using UnityEngine.Networking;
using BitConverter = System.BitConverter;

// http client
public class HttpMgr : Singleton<HttpMgr>
{
    const string url = "https://28un955730.oicp.vip:20272";
    int protocol_length = 0;
    public int Protocol_length
    {
        get
        {
            return protocol_length;
        }
    }
    public override void Init()
    {
        base.Init();
        int id = 99999;
        //convert to bytes
        var bytes = BitConverter.GetBytes(id);
        protocol_length = bytes.Length;
    }
    //HttpPostAsync
    async public UniTask HttpPostAsync(int protocol,byte[] data)
    {
        var bytes = BitConverter.GetBytes(protocol);
        // merge protocol id and protocol data
        byte[] result = new byte[bytes.Length + data.Length];
        bytes.CopyTo(result,0);
        data.CopyTo(result,bytes.Length);
        var request = new UnityWebRequest(url,UnityWebRequest.kHttpVerbPOST);
        request.uploadHandler = new UploadHandlerRaw(result);
        request.downloadHandler = new DownloadHandlerBuffer();
        request.SetRequestHeader("Content-Type","application/octet-stream");
        await request.SendWebRequest();
        if(request.isNetworkError || request.isHttpError)
        {
            Debug.LogError(request.error);
        }
        else
        {
            Debug.Log(request.downloadHandler.text);
            byte[] protocol_result = request.downloadHandler.data;
            // get protocol id
            byte[] id = new byte[protocol_length];
            System.Array.Copy(protocol_result,id,protocol_length);
            int protocol_id = BitConverter.ToInt32(id,0);
            // get protocol data
            byte[] protocol_data = new byte[protocol_result.Length - protocol_length];
            System.Array.Copy(data,protocol_length,protocol_data,0,protocol_data.Length);
            // TODO
            NetMgr.Instance.Dispatch(protocol_id,protocol_data,0);
        }
    }

    // http get
    async public UniTask HttpGetAsync(int protocol,byte[] data)
    {
        var bytes = BitConverter.GetBytes(protocol);
        // merge protocol id and protocol data
        byte[] result = new byte[bytes.Length + data.Length];
        bytes.CopyTo(result,0);
        data.CopyTo(result,bytes.Length);
        var request = new UnityWebRequest(url,UnityWebRequest.kHttpVerbGET);
        request.downloadHandler = new DownloadHandlerBuffer();
        request.SetRequestHeader("Content-Type","application/octet-stream");
        await request.SendWebRequest();
        if(request.isNetworkError || request.isHttpError)
        {
            Debug.LogError(request.error);
        }
        else
        {
            Debug.Log(request.downloadHandler.text);
            byte[] protocol_result = request.downloadHandler.data;
            // get protocol id
            byte[] id = new byte[protocol_length];
            System.Array.Copy(protocol_result,id,protocol_length);
            int protocol_id = BitConverter.ToInt32(id,0);
            // get protocol data
            byte[] protocol_data = new byte[protocol_result.Length - protocol_length];
            System.Array.Copy(data,protocol_length,protocol_data,0,protocol_data.Length);
            // TODO
            NetMgr.Instance.Dispatch(protocol_id,protocol_data,0);
        }
    }

    public void TestProtocol(byte[] data)
    {
        //sub bytes
        var id = new byte[4];
        System.Array.Copy(data,id,4);
        int protocol_id = BitConverter.ToInt32(id,0);
        // get protocol data
        byte[] protocol_data = new byte[data.Length - protocol_length];
        System.Array.Copy(data,protocol_length,protocol_data,0,protocol_data.Length);
        NetMgr.Instance.Dispatch(protocol_id,protocol_data,0);
    }
}